// pages/coach/coach.js
let app = getApp();

let cookie = wx.getStorageSync('cookieKey');
let header = {};
if (cookie) {
  header.Cookie = cookie;
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    animationData: {},
    showShopPopup: false,
    arr: null, // 教练员集合
    coach: null, //教练员信息
    expertSpecia:"",// 专业
    searchValue: '', // 搜索框值
    coachName: null, // 搜索需要提交的值
    showOne: "", // 增加显示数据
    page: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.login();



    let this_ = this;
    this.setData({
      showOne:"点击显示更多",
      page:1
    })
    this.selectAll();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  showModal: function (e) {
    let this_ = this;
    wx.request({
      url: app.globalData.url + 'AtsEmpCoachController/findById',
      header: header,
      data: {
        id: e.currentTarget.dataset.coachid
      },
      success: res => {
        this_.setData({
          coach: res.data
        })
        wx.request({
          url: app.globalData.url + 'atsBaseSportController/findById',
          header: header, 
          data: {
            id: this_.data.coach.expertSpecia
          },
          success: res => {
            this_.setData({
              expertSpecia: res.data.sportName
            })
          }
        })
      }
    })



    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },
  onShareAppMessage() {
    return {
      title: 'scroll-view',
      path: 'page/component/pages/scroll-view/scroll-view'
    }
  },

  data: {
    toView: 'green'
  },

  upper(e) { },

  lower(e) { },

  scroll(e) { },

  scrollToTop() {
    this.setAction({
      scrollTop: 0
    })
  },

  tap() {
    for (let i = 0; i < order.length; ++i) {
      if (order[i] === this.data.toView) {
        this.setData({
          toView: order[i + 1],
          scrollTop: (i + 1) * 200
        })
        break
      }
    }
  },

  tapMove() {
    this.setData({
      scrollTop: this.data.scrollTop + 10
    })
  }, bindButtonTap: function () {
    this.setData({
      focus: true
    })
  },
  bindKeyInput: function (e) {

    this.setData({
      searchValue: e.detail.value
    })
    console.info(this.data.searchValue)
  },
  suo: function (e) {
    this.setData({
      coachName: e.detail.value,
      arr: null
    })

    this.selectAll();
    console.info("查询")

  },
  shows: function (e) {
    this.setData({
      page: this.data.page + 1
    })

    this.selectAll();

  },selectAll:function(){
    wx.showLoading({
      title: '正在加载',
    })
    var this_=this;

    if (!this.data.coachName){
        this.setData({
          coachName:""
        })
    }
    let user = wx.getStorageSync('user');
    if (user.data.manCardid == "superman" || user.data.companyNum == 1) {
      var data = {
        coachName: this.data.coachName,
        page: this.data.page,
        limit: 10
      };
      
    } else if (user.data.companyNum == 2 || user.data.companyNum == 5) {
      data = {
        coachId: user.data.filId,
        coachName: this.data.coachName,
        page: this.data.page,
        limit: 10
      }
    } else {
      wx.hideLoading();
      wx.showToast({
        title: '没有数据了',
        icon: 'none',
        duration: 2000
      })
      return false;
    }

    
    wx.request({
      url: app.globalData.url + 'AtsEmpCoachController/selectAll',
      header: header,
      data:data,
      success(res) {

        if (this_.data.arr != null) {
          if (this_.data.arr.length >= res.data.count) {
            this_.setData({
              showOne: "已显示所有数据"
            })
            wx.hideLoading();
            return false;
          }
          var arrs = this_.data.arr
          for (var i = 0; i < res.data.data.length; i++) {
            arrs.push(res.data.data[i])
          }
          this_.setData({
            arr: arrs
          })
        } else {
          console.info(res.data.data)
          this_.setData({
            arr: res.data.data
          })
        }
        wx.hideLoading();
      }
    })
  }
})